﻿using System;
using System.IO;
using System.Net.Mail;

namespace Syno.Core.Output.OutputFormat
{
    public class EmailFormatOutput : FormatOutputBase, IOutput
    {
        private SmtpClient _smtp;
        private readonly string _from;
        private readonly string _to;
        private readonly bool _isBodyHtml;


        public EmailFormatOutput(SmtpClient smtp, string from, string to)
        {
            _smtp = smtp;
            _to = to;
            _from = from;
            _isBodyHtml = true;
        }

        public EmailFormatOutput(string from, string to, bool isBodyHtml)
        {
            _to = to;
            _from = from;
            _isBodyHtml = isBodyHtml;
        }

        public string Body { get; set; }
        public string Subject { get; set; }

        public string From
        {
            get { return _from; }
        }

        public string To
        {
            get { return _to; }
        }

        public bool IsBodyHtml
        {
            get { return _isBodyHtml; }
        }

        public SmtpClient Smtp
        {
            get { return _smtp; }
            set { _smtp = value ; }
        }

        public override void Process()
        {
            var mailMessage = new MailMessage(_from, _to);
            mailMessage.IsBodyHtml = _isBodyHtml;
            mailMessage.Body = GetTemplatedValue(Body);
            mailMessage.Subject = GetTemplatedValue(Subject);
            _smtp.Send(mailMessage);
        }

        private string GetTemplatedValue(string textReader)
        {
            return Templater.ToString(textReader);
        }
    }
}
